package com.xhwl.centiotdata.suppliersystem.eesd.runpaq;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IEesdCentService;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.energy.EesdElectricDetailDTO;
import com.xhwl.common.dto.cent.energy.EesdElectricDetailDTO.ValueEntity;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.common.utils.MapTool;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RunpaqApiHandler {
	@Autowired
	IDeviceCentService iDeviceCentService;
	@Autowired
    IEesdCentService iEesdCentService;
	
	public void pullRecord(EesdConfigDTO eesdConfigDTO) {
		if (eesdConfigDTO.getProjectId() == null || StringUtils.isBlank(eesdConfigDTO.getElectricHost())) {
			log.warn("pull[RUNPAQ源牌]电表实时数据配置有误");
			return;
		}
		// 查询设备中台全量的仪表台设备
		DevicePageQuery deviceQuery = new DevicePageQuery();
		deviceQuery.setProjectId(eesdConfigDTO.getProjectId());
		deviceQuery.setProductId(6);
		deviceQuery.setDeviceTypeId(DeviceTypeEnum.WATT_HOUR_METER.getCode());
		List<DeviceDTO> listDevice = iDeviceCentService.listDevice(null, deviceQuery);
		// 查询RUNPAQ源牌实时电表数据
		String resp = null;
		if (CollectionUtils.isNotEmpty(listDevice)) {
			String codes = listDevice.stream().filter(d -> StringUtils.isNotBlank(d.getNodeId()))
					.map(DeviceDTO::getNodeId).collect(Collectors.joining(","));
			log.info("pull[RUNPAQ源牌]电表实时数据, param:{}, url :{}", codes, eesdConfigDTO.getElectricHost());
			resp = HttpUtil.post(eesdConfigDTO.getElectricHost(), MapTool.getSOMap("codes", codes));
		}
		// 转换映射关系
//		{
//		   "data" : [
//		      {
//		         "倍率" : 1.0,
//		         "采集时间" : "2021-10-05T15:40:22",
//		         "服务器时间" : "2021-10-05T15:40:22",
//		         "区域名称" : "天面~16层",
//		         "设备ID" : 28,
//		         "设备编号" : "cj001-12437",
//		         "设备名称" : "25F电表-1",
//		         "是否已存入数据库" : "是",
//		         "数据类型" : "实时",
//		         "项目编号" : "HHD-001",
//		         "正向有功" : 1961.66
//		      }
//		   ]
//		}
		List<EesdElectricDetailDTO> list = null;
		JSONObject respJson = JSONObject.parseObject(resp);
		if (respJson != null) {
			list = Lists.newArrayList();
			JSONArray electricList = respJson.getJSONArray("data");
			if (electricList != null && !electricList.isEmpty()) {
				for (Object obj : electricList) {
					list.add(parseElectric(eesdConfigDTO.getProjectId(), (JSONObject) obj));
				}
			}
		}
		// 调eesd保存电表数据
		if(CollectionUtils.isNotEmpty(list)) {
			log.info("获取[RUNPAQ源牌]电表实时数据, 入库", JSONObject.toJSONString(list));
			iEesdCentService.baDeviceDetailSync(list);
		}
	}
	
	private EesdElectricDetailDTO parseElectric(Integer projectId, JSONObject objJson) {
		EesdElectricDetailDTO dto = new EesdElectricDetailDTO();
		dto.setProjectId(projectId);
		dto.setDeviceId(objJson.getString("设备编号"));
		dto.setTime(new Date());
		dto.setRate(objJson.getString("倍率") != null ? Double.valueOf(objJson.getString("倍率")):1.0);
		dto.setDeviceType(DeviceTypeEnum.WATT_HOUR_METER.getCode());
		dto.setJsonFieldFlag(false);
		ValueEntity value = new EesdElectricDetailDTO.ValueEntity();
		value.setValue(objJson.getString("正向有功") != null ? Double.valueOf(objJson.getString("正向有功")):0.0);
		value.setValue(value.getValue()*dto.getRate());
		dto.setValue(value);
		return dto;
	}
	
	
//	public static void main(String[] args) {
//		String resp = "{\"data\":[{\"是否已存入数据库\":\"是\",\"采集时间\":\"2021-10-05T15:40:22\",\"区域名称\":\"天面~16层\",\"服务器时间\":\"2021-10-05T15:40:22\",\"正向有功\":1961.66,\"设备编号\":\"cj001-12437\",\"项目编号\":\"HHD-001\",\"设备名称\":\"25F电表-1\",\"数据类型\":\"实时\",\"倍率\":1.0,\"设备ID\":28},{\"是否已存入数据库\":\"是\",\"采集时间\":\"2021-10-05T15:40:33\",\"区域名称\":\"天面~16层\",\"服务器时间\":\"2021-10-05T15:40:33\",\"正向有功\":99.6,\"设备编号\":\"cj001-12332\",\"项目编号\":\"HHD-001\",\"设备名称\":\"25F电表-2\",\"数据类型\":\"实时\",\"倍率\":20.0,\"设备ID\":29}]}";
//		List<EesdElectricDetailDTO> list = Lists.newArrayList();
//		JSONObject respJson = JSONObject.parseObject(resp );
//		if(respJson != null) {
//			JSONArray electricList = respJson.getJSONArray("data");
//			if(electricList != null && !electricList.isEmpty()) {
//				for (Object obj : electricList) {
//					list.add(parseElectric(333, (JSONObject)obj));
//				}
//			}
//		}
//		System.out.println(JSONObject.toJSONString(list));
//	}

}
